package com.crayon.account.fragments;

import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

import com.crayon.account.ItemDetailFragment;
import com.crayon.account.dao.DAOAccount;
import com.crayon.account.item.adapter.AdapterDaoAccountSingle;
import com.crayon.account.loader.LoaderSQLAccountSingle;
import com.crayon.sharemenu.R;
import com.crayon.util.GDB;
import com.crayon.util.UIBIND;
import com.crayon.util.UTILSC;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class FragmentRootSearch extends ItemDetailFragment 
implements OnItemClickListener
,OnQueryTextListener
,LoaderManager.LoaderCallbacks<List<DAOAccount>>
{

	AdapterDaoAccountSingle mAdapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_list_simple,container, false);
		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		
//		ActionBar actionBar = getActivity().getActionBar();
//	    SearchView searchView = new SearchView(getActivity());
//	    actionBar.setCustomView(searchView);
//	    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//	    searchView.setQuery("test",true);
//	    searchView.setFocusable(true);
//	    searchView.setIconified(false);
//	    searchView.requestFocusFromTouch();
	    
		SearchView searchView = (SearchView)UIBIND.bindView(getView(), R.id.searchView);
		searchView.setVisibility(View.VISIBLE);
		searchView.setOnQueryTextListener(this);
	    searchView.setQuery("",true);
	    searchView.setFocusable(true);
	    searchView.setIconified(false);
	    searchView.requestFocusFromTouch();

		ListView listView = (ListView)getView().findViewById(R.id.listView);
		
		mAdapter = new AdapterDaoAccountSingle(getActivity());
		listView.setAdapter(mAdapter);
		listView.setOnItemClickListener(this);
		
		getLoaderManager().initLoader(0, null, this);

		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		ListView listView = (ListView)getView().findViewById(R.id.listView);
		DAOAccount item = (DAOAccount)listView.getAdapter().getItem(position);
		UTILSC.addOnPopWriteFragment(getActivity(),item,null);
	}
	
	
	

	@Override
	public boolean onQueryTextChange(String newText) {
		// TODO Auto-generated method stub
		GDB.log(getClass(), "onQueryTextChange >> " + newText);
		queryCompose(newText);
		return false;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		// TODO Auto-generated method stub
		GDB.log(getClass(), "onQueryTextSubmit >> " + query);
		queryCompose(query);
		return false;
	}

	
	private void queryCompose(String newText){
		
		for(char c : newText.toCharArray()){
			GDB.log(getClass(), " SECH " + c +"\t"+ Integer.toHexString(c));
		}
		
		Bundle bundle =new Bundle();
		bundle.putString("query", newText);
		if(getLoaderManager().getLoader(0)!=null){
			getLoaderManager().restartLoader(0, bundle, this);
		}
	}
	@Override
	public Loader<List<DAOAccount>> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		String query = arg1!=null ? arg1.getString("query"):"";
		return new LoaderSQLAccountSingle(getActivity(), query);
	}

	@Override
	public void onLoadFinished(Loader<List<DAOAccount>> arg0,
			List<DAOAccount> arg1) {
		// TODO Auto-generated method stub
		if(arg1!=null && arg1.size()>0){
			if(arg0 instanceof LoaderSQLAccountSingle){
				String query = ((LoaderSQLAccountSingle)arg0).getQuery();
				if(!StringUtils.isEmpty(query))mAdapter.setQuery(query.trim());
			}
			mAdapter.clear();
			mAdapter.addAll(arg1);
			mAdapter.notifyDataSetChanged();
		}
		
	}

	@Override
	public void onLoaderReset(Loader<List<DAOAccount>> arg0) {
		// TODO Auto-generated method stub
		
	}


}
